<?php
  
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

Route::get('/', function()
{
	return View::make('start');
});

Route::get('start', function()
{
	return View::make('start');
});

Route::get('users', function()
{
    $users = User::all();
    return View::make('users')->with('users', $users);
});

Route::get('fields', function()
{
    $fields = Field::all();
    return View::make('fields')->with('fields', $fields);
});

Route::get('register', function()
{
    if (Auth::check()) {
        return View::make('error')->with('err', 'You cannot register while logged on');        
    }
    return View::make('register')->with('user', User::defaultUser());
});

Route::get('info', function()
{
    $user = User::defaultUser();
    if (Auth::check()) {
        $user = Auth::user();
        return View::make('register')->with('user', $user);
    }
});

Route::get('login', function()
{
    if (Auth::check()) {
        return View::make('error')->with('err', 'You are logged in already');
    }
    $username = Session::get('username', 'xxx@xxx.com');
    return View::make('login')->with('username', $username);
});

Route::get('logout', function()
{
    return View::make('logout');
});

Route::get('error', function($err = null) 
{
    if ($err == null) {
        $err = Session::get('err');
    }
    return View::make('error')->with('err', $err);
});

Route::get('user/{id}', function($id)
{
    $user = User::firstByAttributes(array('id' => $id));
    if ($user == null) {
        return View::make('error')->with('err', 'User '.$id.' not found');
    }
    return View::make('user')->with('user', $user);
});

Route::get('team/{id}', function($id)
{
    $team = Team::firstByAttributes(array('id' => $id));
    if ($team == null) {
        return View::make('error')->with('err', 'Team '.$id.' not found');
    }
    return View::make('team')->with('team', $team);
});

Route::get('teams/{uid}', function($uid)
{
    $teams  = Team::all();
    $user   = User::firstByAttributes(array('id' => $uid));
    if ($user == null) {
        return View::make('error')->with('err', 'User '.$uid.' not found');
    }
    return View::make('teams')->with(array('teams' => $teams, 'user' => $user));
});

Route::get('teams', function()
{
    $teams  = Team::all();
    $user   = Auth::user();
    if ($user == null) {
        return View::make('teams')->with(array('teams' => $teams, 'user' => $user));
    }
    return Redirect::to('teams/' . $user->getKey());
});

// the routes are not important as long as they are unique
Route::post('handlerlogin'    , 'XHandler@login'    );
Route::post('handlerregister' , 'XHandler@register' );
Route::post('handlerupdate'   , 'XHandler@update'   );
Route::post('handlerlogout'   , 'XHandler@logout'   );

//Route::get('zzz', 'XHandler@login');

   
